package com.push.example;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;
import com.push.example.model.DeviceSecureStorage;

@SuppressWarnings("unchecked")
public class SendMessage extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter out = resp.getWriter();
		PersistenceManager pm = Register.pmfInstance.getPersistenceManager();
		out.print("<html><body>");
		
		
		String sub = req.getParameter("sub");
		if (sub != null && sub != "") {
			String device = req.getParameter("device");
			String text = req.getParameter("text");
			if (device != null && device != "" && text != null && text != "") {
				DeviceSecureStorage dev = pm.getObjectById(
						DeviceSecureStorage.class, device);
				if (dev != null) {
					Sender sender=new Sender("AIzaSyCRlias5cXSRKcG7J1CJDRM33_0jd6sRRY");
					Message message = new Message.Builder().addData("message", text).build();
					Result result = sender.send(message, dev.getRegistrationId(), 3);
					
					out.println("<span >getMessageId " + result.getMessageId()+ "</span>");
					out.println("<span >getErrorCodeName " + result.getErrorCodeName() + "</span>");
					out.println("<span >getCanonicalRegistrationId " + result.getCanonicalRegistrationId() + "</span>");
					
				} else {
					out.println("<span style='color:red'>Device not found</span>");
				}
			} else {
				out.println("<span style='color:red'>Parametreler Hatalı</span>");
			}
		}

		try {
			String query = "select from " + DeviceSecureStorage.class.getName();
			List<DeviceSecureStorage> devices = (List<DeviceSecureStorage>) pm
					.newQuery(query).execute();
			out.print("<form action='' method='get'>");
			out.print("<select name='device'>");
			for (int i = 0; devices != null && i < devices.size(); i++) {
				out.println("<option value=" + devices.get(i).getDeviceId()
						+ ">" + devices.get(i).getDeviceId() +"</option>");
			}
			out.print("</select>");

			out.print("<input type='text-area' name='text'></input>");
			out.print("<input type='submit' name='sub' value='Gonder'></input>");

			out.print("</form></body></html>");
		} catch (Exception e) {
			out.println("<result error='true'>" + e.toString() + "</result>");
		}
	}

	public static String readInputStreamAsString(InputStream in)
			throws IOException {

		BufferedInputStream bis = new BufferedInputStream(in);
		ByteArrayOutputStream buf = new ByteArrayOutputStream();
		int result = bis.read();
		while (result != -1) {
			byte b = (byte) result;
			buf.write(b);
			result = bis.read();
		}
		return buf.toString();
	}

}
